package com.jzg.mvc.api.cabinet;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import com.jfinal.aop.Before;
import com.jfinal.core.Path;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.CabPicture;
import com.jzg.common.model.sys.SysUser;
import com.jzg.common.util.FileUtil;
import com.jzg.common.util.UuidUtil;


@Path(value = "/api/cab/picture", viewPath = "")
public class PictureController extends BaseController{

	public void getCabPicture() {
		List<CabPicture> pictures = CabPicture.dao.findAll();
		renderSuccess("查询成功！", pictures);
	}
	
	private String rootPath = PathKit.getWebRootPath();
	@Before(POST.class)
	public void upload() {
		List<UploadFile> uploadFiles = getFiles();
		
		if(uploadFiles == null || uploadFiles.size()==0) {
			renderError("参数不完整！");
			return;
		}
		
		try {
			String userId = getPara("userId");
			SysUser sysUser = SysUser.dao.findById(userId);
			if(sysUser==null) {
				renderError("非法操作用户！");
				return ;
			}
			
			int count = 0;
			for (UploadFile uploadFile : uploadFiles) {
				String path = "/upload/cabPicture/" + uploadFile.getFileName().replace(" ", "");
				File file = new File(rootPath+path);
				if (!file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}
				if (file.exists()) {
					file.delete();
				}
				java.awt.image.BufferedImage image = ImageIO.read(uploadFile.getFile());
				double width = image.getWidth();
				double height = image.getHeight();
				uploadFile.getFile().renameTo(file);
				CabPicture cabPicture = new CabPicture();
				String id = UuidUtil.getUUID();
				cabPicture.setId(id);
				//cabPicture.setCabId(cabId);
				//cabPicture.setName(uploadFile.getFileName());
				cabPicture.setName(id);
				cabPicture.setWidth(width);
				cabPicture.setHeight(height);
				cabPicture.setUrl(path);
				cabPicture.setFormat(FileUtil.lastName(file));
				cabPicture.setCreateTime(new Date(System.currentTimeMillis()));
				cabPicture.setCreateUserId(sysUser.getID());
				cabPicture.setCreateUserName(sysUser.getNAME());
				if(cabPicture.save()) {
					count ++;
				}
			}
			
			if(count>0) {
				renderSuccess("共【"+uploadFiles.size()+"】个文件，上传成功【"+count+"】个文件，失败【"+(uploadFiles.size()-count)+"】");
			}else {
				renderError("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
	public void delete() {
		CabPicture model = CabPicture.dao.findById(this.getPara("id"));
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！");
				if(!StrKit.isBlank(model.getUrl())) {
					File file = new File(rootPath+model.getUrl());
					if (file.exists()) {
						file.delete();
					}
				}
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
}
